/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:15:44
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-07-11 09:51:21
*/

import moment from 'moment'

import Pages from '../../component/pages/pages.vue'
import Uploader from '../item-uploader/item-uploader.vue'

import { showMsg, getCastTalkList, deleteCastTalk } from '../../vuex/action'

export default {
  name: 'cast-talk-item-list',
  components: {
    'vue-pages': Pages,
    'vue-uploader': Uploader
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.castTalkNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetlist: getCastTalkList,
      xDelete: deleteCastTalk
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetlist({
        start: start,
        limit: limit
      })
        .then((res) => {
          this.list = res.data.msgSimpleResponses
          this.pages.totalPages = Math.ceil(res.data.total / this.pages.limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
    showUploader(id) {
      var ref = this.$refs.uploader
      ref.init(id)
    },
    updateItem(item) {
      let ref = this.$root.$refs.itemConfig
      let {id, pubDate, title, body, tags, voiceInfoDtos, speakerInfos, meetingSummary} = item

      pubDate = new Date(pubDate)
      pubDate = moment(pubDate).format('YYYY-MM-DD HH:mm:ss')

      ref.form = {
        id: id,
        pubDate: pubDate,
        title: title,
        body: body,
        tags: tags.join(', '),
        voiceDuration: voiceInfoDtos[0].duration,
        voicePicUrl: voiceInfoDtos[0].voicePicUrl,
        voiceUrl: voiceInfoDtos[0].voiceUrl,
        speaker: speakerInfos[0].speaker,
        speakerCompany: speakerInfos[0].company,
        speakerJobTitle: speakerInfos[0].jobTitle,
        meetingSummary: meetingSummary
      }
      ref.actionType = 'UPDATE'
      ref.showModal = true
    },
    deleteItem(id) {
      this.xDelete({
        id: id
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
